package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import bd.Servicios;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class VentanaExtraer extends javax.swing.JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jPanel1;
	private JLabel lblCajero;
	private JButton btnCancel;
	private JButton btnAceptar;
	private JTextField tfMonto;
	private JLabel lblMonto;
	private JLabel lblTarjeta;
	private Servicios serv;
	private int cod_caja = 100;

	/**
	 * Auto-generated main method to display this JInternalFrame inside a new
	 * JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		VentanaExtraer inst = new VentanaExtraer();
		JDesktopPane jdp = new JDesktopPane();
		jdp.add(inst);
		jdp.setPreferredSize(inst.getPreferredSize());
		frame.setContentPane(jdp);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public VentanaExtraer() {
		super();
		serv = Servicios.getServicios();
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(595, 387));
			this.setBounds(0, 0, 595, 387);
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				jPanel1.setPreferredSize(new java.awt.Dimension(375, 133));
				jPanel1.setLayout(null);
				{
					lblTarjeta = new JLabel();
					jPanel1.add(lblTarjeta);
					lblTarjeta.setText("Tarjeta:");
					lblTarjeta.setBounds(12, 19, 145, 26);
				}
				{
					lblMonto = new JLabel();
					jPanel1.add(lblMonto);
					lblMonto.setText("Monto: ");
					lblMonto.setBounds(12, 62, 110, 16);
				}
				{
					tfMonto = new JTextField();
					jPanel1.add(tfMonto);
					tfMonto.setBounds(134, 59, 109, 23);

				}
				{
					btnAceptar = new JButton();
					jPanel1.add(btnAceptar);
					btnAceptar.setText("Aceptar");
					btnAceptar.setBounds(51, 110, 82, 23);

					btnAceptar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							btnAceptarActionPerformed(evt);
						}
					});
				}
				{
					btnCancel = new JButton();
					jPanel1.add(btnCancel);
					btnCancel.setText("Cancelar");
					btnCancel.setBounds(169, 110, 95, 23);
					btnCancel.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							btnCancelActionPerformed(evt);
						}
					});
				}
				{
					lblCajero = new JLabel();
					jPanel1.add(lblCajero);
					lblCajero.setText("Cajero: ");
					lblCajero.setBounds(133, 22, 110, 20);
				}
			}
			setVisible(true);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void actualizarDatos() {
		lblTarjeta.setText("Tarjeta: " + serv.getTarjeta());
		lblCajero.setText("Cajero: " + cod_caja);
	}

	private void btnAceptarActionPerformed(ActionEvent evt) {

		String r = serv
				.extraer(Float.parseFloat(tfMonto.getText().replace(",", ".")),
						cod_caja);

		tfMonto.setText("");
		if (r.equals("La extraccion se realizo con exito"))
			JOptionPane.showMessageDialog(this, r + ". Saldo actual: " + serv.getSaldo(serv.getTarjeta()),
					"Operacion Exitosa.",
					JOptionPane.INFORMATION_MESSAGE);
		else
			JOptionPane.showMessageDialog(this, r, "Hubo Un Error",
					JOptionPane.INFORMATION_MESSAGE);
	}

	private void btnCancelActionPerformed(ActionEvent evt) {
		this.setVisible(false);
		try {
			this.setMaximum(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}